﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2021_5_31
{
    class Program
    {
        static Dictionary<int,Student> student = new Dictionary<int,Student>();

        static void Main(string[] args)
        {
            Student s1 = new Student(1,"张三",20);
            Student s2 = new Student(3,"李四",17);
            student.Add(s1.StudentID,s1);
            student.Add(s2.StudentID, s2);
            Welcome();
        }

        public static void Welcome()
        {
            Console.WriteLine("请选择：1、添加学生信息。2、查询学生信息。");
            int input = int.Parse(Console.ReadLine());

            switch (input)
            {
                case 1:
                    AddStudent();
                    break;
                case 2:
                    SelStudent();
                    break;
                default:
                    Console.WriteLine("输入有误，请重新输入！");
                    Welcome();
                    break;
            }
        }

        public static void SelStudent()
        {
            Console.WriteLine("1、查询所有（按学号排序）2、查询所有（按姓名排序）3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出");
            int input = int.Parse(Console.ReadLine());

            switch (input)
            {
                case 1:
                    List<Student> IdStu = student.Values.ToList();
                    IdStu.Sort(new IdSort());
                    PrintList(IdStu);
                    Welcome();
                    break;
                case 2:
                    List<Student> NameStu = student.Values.ToList();
                    NameStu.Sort(new NameSort());
                    PrintList(NameStu);
                    Welcome();
                    break;
                case 3:
                    List<Student> AgeStu = student.Values.ToList();
                    AgeStu.Sort(new AgeSort());
                    PrintList(AgeStu);
                    Welcome();
                    break;
                case 4:
                    Console.WriteLine("请输入要查询学生的学号：");
                    int id = int.Parse(Console.ReadLine());
                    if (student.ContainsKey(id))
                    {
                        Console.WriteLine(student[id]);
                    }
                    else
                    {
                        Console.WriteLine("查无此学生！");
                        Welcome();
                    }
                    break;
                case 5:
                    break;
                default:
                    Console.WriteLine("输入有误！");
                    Welcome();
                    break;
            }
        }

        private static void PrintList(List<Student> list)
        {
            foreach (Student s in list)
            {
                Console.WriteLine(s);
            }

        }

        public static void AddStudent()
        {
            Student s = new Student();

            Console.WriteLine("请输入学生的学号：");
            s.StudentID = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入学生姓名：");
            s.Name = Console.ReadLine();
            Console.WriteLine("请输入学生年龄：");
            s.Age = int.Parse(Console.ReadLine());

            if (student.ContainsKey(s.StudentID))
            {
                Console.WriteLine("重复的学号！");
            }
            else
            {
                student[s.StudentID] = s;
            }

            Welcome();
            
        }
    }
}
